package rise.net.handler;

import rise.comunity.Community;
import rise.comunity.Status;
import rise.comunity.User;
import rise.message.Message;
import rise.ui.Conversation;
import rise.ui.MapActivity;
import rise.ui.UserListActivity;

public class MessageHandler implements Runnable {

	private Message message;
	public MessageHandler(String packetRappresentation) {
		this.message = Message.createMessage(packetRappresentation);
	}

	@Override
	public void run() {
		String sender = this.getSender();
		Status senderStatus = this.getSenderStatus();
		String text = message.getText();
		double lat = message.getLat(), lon = message.getLon();
		
		Community community = Community.obtainCommunity();
		if(!community.contains(sender))
			community.addUser(new User(senderStatus, sender, lat, lon));
		
		community.setUserStatus(sender, senderStatus);
		community.setUserGPSLocation(sender, lat, lon);
		community.addMessageFrom(sender, text);
		
		UserListActivity.handler.sendEmptyMessage(0);
		Conversation.handler.sendEmptyMessage(0);
		MapActivity.handler.sendEmptyMessage(0);
	}
	
	public String getSender(){
		return message.getSender();
	}
	private Status getSenderStatus(){
		return message.getStatus();
	}

}
